package properties;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.WorkDayUtil;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * @author liufei
 * @date 2021/8/9 10:47
 */
@Data
public class AutoBGProperties extends BaseProperties {

    private static final Logger logger = LoggerFactory.getLogger(AutoBGProperties.class);
    private String user;
    private String password;
    private String projectName;
    private String firstDay;

    private String reportForWorkType;
    private String selectTask;
    private String selectTaskType;
    private String workType;

    private WorkTimes workTimes;

    private AutoBGProperties() {

    }

    public static AutoBGProperties readProperties() {
        Properties properties = readProperties("config.properties");
        AutoBGProperties autoBGProperties = new AutoBGProperties();
        autoBGProperties.user = properties.getProperty("user");
        autoBGProperties.password = properties.getProperty("password");
        autoBGProperties.projectName = properties.getProperty("project_name");
        autoBGProperties.firstDay = properties.getProperty("first_day");
        autoBGProperties.reportForWorkType = properties.getProperty("report_for_work_type");
        autoBGProperties.selectTask = properties.getProperty("select_task");
        autoBGProperties.selectTaskType = properties.getProperty("select_task_type");
        autoBGProperties.workType = properties.getProperty("work_type");
        autoBGProperties.workTimes = readWorkTimes(properties);
        logger.info("加载配置文件");
        logger.info(autoBGProperties.toString());
        return autoBGProperties;
    }

    private static WorkTimes readWorkTimes(Properties properties) {
        WorkTimes workTimes = new WorkTimes();
        Field[] fields = WorkTimes.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            WorkTimeBean workTimeBean = new WorkTimeBean(
                    properties.getProperty(field.getName())
                    , properties.getProperty(field.getName() + "_jb")
                    , properties.getProperty(field.getName() + "_word_context"));
            try {
                field.set(workTimes, workTimeBean);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return workTimes;
    }

    public static AutoBGProperties readPropertiesByName(User user) {
        Properties properties = readProperties(user.getName() + "-config.properties");
        if (properties == null) {
            return newAllAutoProperties(user);
        }
        AutoBGProperties autoBGProperties = new AutoBGProperties();
        autoBGProperties.user = user.getName();
        autoBGProperties.password = user.getPwd();
        autoBGProperties.projectName = user.getProjectName();
        autoBGProperties.firstDay =  WorkDayUtil.getWeekDate(1);
        autoBGProperties.reportForWorkType = user.getReportForWorkType();
        autoBGProperties.selectTaskType = "auto";
        autoBGProperties.workType = user.getWorkType();
        autoBGProperties.workTimes = readWorkTimes(properties);
        logger.info("加载配置文件");
        logger.info(autoBGProperties.toString());
        return autoBGProperties;
    }

    public static AutoBGProperties newAllAutoProperties(User user) {
        AutoBGProperties autoBGProperties = new AutoBGProperties();
        autoBGProperties.user = user.getName();
        autoBGProperties.password = user.getPwd();
        autoBGProperties.projectName = user.getProjectName();
        autoBGProperties.firstDay = WorkDayUtil.getWeekDate(1);
        autoBGProperties.reportForWorkType = "all_auto";
        autoBGProperties.selectTaskType = "auto";
        autoBGProperties.workType = user.getWorkType();
        logger.info("创建全自动配置");
        logger.info(autoBGProperties.toString());
        return autoBGProperties;
    }


}
